package com.example.demo.controller;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.DfyzWebsiteRole;
import com.example.demo.bean.Entity;
import com.example.demo.dao.DfyzWebsiteRoleMapper;


@CrossOrigin(allowCredentials = "true", origins = "*", maxAge = 3600, methods = { RequestMethod.GET, RequestMethod.POST,
		RequestMethod.PUT,
		RequestMethod.OPTIONS }, allowedHeaders = { "x-requested-with", "accept", "authorization", "content-type" })
@RestController
public class Controller {
	protected final Logger logger = LogManager.getLogger(this.getClass());

	@Autowired
	RedisTemplate redis;
	
	@Autowired
	private StringRedisTemplate stringRedis;

	@Autowired
	DfyzWebsiteRoleMapper dfyzWebsiteRole;

	@RequestMapping("/log")
	public Object log(HttpServletRequest request, HttpServletResponse response) {
		logger.debug("aaa");
		Map<String, Object> params = new HashMap<String, Object>();
		int a = 10 / 0;
		int selectDfyzWebsiteRoleCount = dfyzWebsiteRole.selectDfyzWebsiteRoleCount(params);
		return selectDfyzWebsiteRoleCount;
	}

	@RequestMapping("/log2")
	public Object log2(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> params = new HashMap<String, Object>();
		int selectDfyzWebsiteRoleCount = dfyzWebsiteRole.selectDfyzWebsiteRoleCount(params);
		return selectDfyzWebsiteRoleCount;
	}

	@RequestMapping("/redis")
	public Object redis(HttpServletRequest request, HttpServletResponse response) {
		stringRedis.opsForValue().set("String", "Stirngd的测试");
		List<String> list = new ArrayList<String>();
		list.add("b1");
		list.add("b2");
		list.add("b3");
		list.add("b4");
		stringRedis.opsForList().rightPushAll("listl", list);
		Map map = new HashMap<String, String>();
		map.put("a1", "a11");
		map.put("a2", "a22");
		map.put("a3", "a33");
		map.put("a4", "a44");
		map.put("a5", "a55");
		map.put("a6", "a66");
		stringRedis.opsForHash().putAll("map", map);
		entiy.setAddress("深圳市南山区太子路工业三路招商局港口大厦");
		entiy.setAge("12");
		entiy.setName("what's Ur name");
//		stringRedis.opsForValue().set("entitiy", entiy);
		return null;
	}

	@RequestMapping("/redis2")
	public Object redis2(HttpServletRequest request, HttpServletResponse response) {
		redis.opsForValue().set("String2", "Stirngd的测试");
		List<String> list = new ArrayList<String>();
		list.add("b1");
		list.add("b2");
		list.add("b3");
		list.add("b4");
		redis.opsForList().rightPushAll("list2", list);
		Map map = new HashMap<String, String>();
		map.put("a1", "map的第一版");
		map.put("a2", "map的第二版");
		map.put("a3", "map的第三版");
		map.put("a4", "map的秀儿");
		map.put("a5", "map你才是秀儿");
		map.put("a6", "你是品如的衣柜");
		String  key = "KEY";
		redis.opsForHash().put("map2", key, map);
		entiy.setAddress("深圳市南山区太子路工业三路招商局港口大厦");
		entiy.setAge("12");
		entiy.setName("what's Ur name");
		redis.opsForValue().set("entity2", entiy);
		return null;
	}

	@Autowired
	Entity entiy;
	
	@RequestMapping("/json")
	public Object json(HttpServletRequest request,HttpServletResponse reponse) {
//		Map map = new HashMap<String, String>();
//		map.put("a1", "a11");
//		map.put("a2", "a22");
//		map.put("a3", "a33");
//		map.put("a4", "a44");
//		map.put("a5", "a55");
//		map.put("a6", "a66");
//		redis.opsForHash().putAll("map2", map);
		//redis map只能分开使用
		Map<String,Object> map = new HashMap<String,Object>();
//		Object o = redis.opsForHash().entries("map2");
//		map.put("redis", o);
//		Object o2 = stringRedis.opsForHash().entries("map");
//		map.put("redisString", o2);
		
//		Object object = redis.opsForValue().get("String2");
		//stringRides string都可以取出来，但是会带个引号
//		Object o2 = stringRedis.opsForValue().get("String2");
//		Object O3 = stringRedis.opsForValue().get("String");
//		map.put("1", o2);
//		map.put("2", O3);
		//stringRides list都可以取出来，但是会带个引号
//		List<String> range = stringRedis.opsForList().range("listl", 0, -1);
//		List<String> range2 = redis.opsForList().range("list2", 0, -1);
//		map.put("1", range);
//		map.put("2", range2);
		//对象的都可以取出来 Stringrides会带引号
		String string = stringRedis.opsForValue().get("entity2");
		Object string2 = redis.opsForValue().get("entity2");
		map.put("1", string);
		map.put("2", string2);
//		Map map = new HashMap<String, String>();
//		map.put("a1", "map的第一版");
//		map.put("a2", "map的第二版");
//		map.put("a3", "map的第三版");
//		map.put("a4", "map的秀儿");
//		map.put("a5", "map你才是秀儿");
//		map.put("a6", "你是品如的衣柜");
//		String  key = "KEY";
//		redis.opsForHash().putAll("map3", map);
//		Object aa = redis.opsForHash().entries("map3");
		
//		Object string = redis.opsForValue().get("String");
		return map;
	}
	//序列化 
    public static byte [] serialize(Object obj){
        ObjectOutputStream obi=null;
        ByteArrayOutputStream bai=null;
        try {
            bai=new ByteArrayOutputStream();
            obi=new ObjectOutputStream(bai);
            obi.writeObject(obj);
            byte[] byt=bai.toByteArray();
            return byt;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    //反序列化
    public static Object unserizlize(byte[] byt){
        ObjectInputStream oii=null;
        ByteArrayInputStream bis=null;
        bis=new ByteArrayInputStream(byt);
        try {
            oii=new ObjectInputStream(bis);
            Object obj=oii.readObject();
            return obj;
        } catch (Exception e) {
            
            e.printStackTrace();
        }
    
        
        return null;
    }
}
